<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/10/25
 * Time: 14:27
 */

namespace app\admin\controller;



use think\Db;
use think\Request;

class Banner extends Base
{
    /**
     * 首页轮播图展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            if(isset($params['banner_title']) && $params['banner_title'] != ''){
                $where['banner_title'] = ['like', '%' . $params['banner_title'] . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_banner')->where($where)->count();// 获取总条数
            $lists = Db::table('up_banner')
                ->field('banner_id, banner_title, banner_label, banner_image, banner_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('banner_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            if($params['banner_type'] == 1){
                if(empty($params['banner_content'])){
                    return json(['code' => 100, 'msg' => '请填写详情']);
                }
            }
            
            $insert = [
                'banner_title' => $params['banner_title'],
                'banner_label' => $params['banner_label'],
                'banner_image' => $params['banner_image'],
                'banner_type' => $params['banner_type'],
                'banner_desc' => $params['banner_desc'],
                'banner_time' => date('Y-m-d H:i:s'),
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            if($params['banner_type'] == 1){
                $insert['banner_content'] = $params['banner_content'];
            }else{
                $insert['banner_link'] = $params['banner_link'];
            }
            $res = Db::table('up_banner')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('banner/banneradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        $banner_id = input('banner_id');
        if(Request::instance()->isPost()){
            $params = input('param.');
            if($params['banner_type'] == 1){
                if(empty($params['banner_content'])){
                    return json(['code' => 100, 'msg' => '请填写详情']);
                }
            }
            
            $update = [
                'banner_title' => $params['banner_title'],
                'banner_label' => $params['banner_label'],
                'banner_type' => $params['banner_type'],
                'banner_desc' => $params['banner_desc'],
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            if(!empty($params['banner_image'])){
                $update['banner_image'] = $params['banner_image'];
            }
            if($params['banner_type'] == 1){
                $update['banner_content'] = $params['banner_content'];
            }else{
                $update['banner_link'] = $params['banner_link'];
            }
            $res = Db::table('up_banner')->where('banner_id', $banner_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner = Db::table('up_banner')
            ->field('banner_title, banner_label, banner_image, banner_content, banner_desc, 
            banner_type, banner_link')
            ->where('banner_id', $banner_id)
            ->find();
        $this->assign([
            'banner_id' => $banner_id,
            'banner' => $banner
        ]);
        return $this->fetch('banner/banneredit');
    }
    
    /**
     * 轮播图详情
     * @return mixed
     */
    public function bannerDetail(){
        $banner_id = input('banner_id');
        $banner = Db::table('up_banner')
            ->field('banner_title, banner_label, banner_image, banner_content, banner_desc, banner_type, 
            banner_link')
            ->where('banner_id', $banner_id)
            ->find();
        $this->assign([
            'banner' => $banner
        ]);
        return $this->fetch('banner/bannerdetail');
    }
    
    /**
     * 删除轮播图
     * @return \think\response\Json
     */
    public function bannerDel(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}